package vip.zhenzicheng.jvm.jvmbook.part2;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 方法区OOM
 * <p>JDK 7 -> VM Args：-XX:PermSize=10M -XX:MaxPermSize=10M
 * <p>JDK 8 -> 见笔记说明
 *
 * @author zhenzicheng
 * @date 2022-07-18 18:01
 */
public class JavaMethodAreaOOM {

  public static void main(String[] args) {
    while (true) {
      Enhancer enhancer = new Enhancer();
      enhancer.setSuperclass(OOMObject.class);
      enhancer.setUseCache(false);
      enhancer.setCallback(new MethodInterceptor() {
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
          return methodProxy.invokeSuper(o, args);
        }
      });

      OOMObject proxyObj = (OOMObject) enhancer.create();
    }
  }

  static class OOMObject{
    public void test() {
      System.out.println("hello");
    }
  }
}
